﻿using CentralVacuum.Common;
using CentralVacuum.Models;
using CentralVacuum.Mqtt;
using HslCommunication;
using HslCommunication.MQTT;
using HslCommunication.Profinet.Melsec;
using HslCommunication.Reflection;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace CentralVacuum.Communication
{
    public static class HslMelsecMCBinary
    {
        #region "Private Section"
        private static int con_flag = 0;

        #endregion

        #region "Public Section"
        public static MelsecMcNet melsecMc;
        public static MqttServer server;
        public static ConcurrentQueue<Action<MelsecMcNet>> writeActions;
        //
        public static OperateResult connect;

        private static string appliance;
        #endregion
        //*************X区域**************

        public static void OpenConnect(string ip, int port)
        {
            IniFile ini = new IniFile(@"C:\FangYuanData\Setting.ini");
            appliance = ini.ReadString("System", "Appliance", "");

            if (appliance.ToLower() == "server")//本地服务端
            {
                melsecMc = new MelsecMcNet(ip, port);//连接plc
                // 如果网络不太理想，配置了5个备用端口，一个有问题，立即切换另一个，可以配置如下的代码
                melsecMc.GetPipeSocket().SetMultiPorts(new int[] { port, 1026, 1027, 1028, 1029, 1030 });
                melsecMc.ConnectTimeOut = 2000; // 连接超时2秒
                melsecMc.ReceiveTimeOut = 2000; // 报文接收超时2秒
                //melsecMc.SetPersistentConnection();  // 设置长链接;
                connect = melsecMc.ConnectServer(); //连接后自动切换到长连接

                writeActions = new ConcurrentQueue<Action<MelsecMcNet>>();   // 所有写入数据的集合，使用生产者，消费者的机制实现
                //
                if (connect.IsSuccess)
                {
                    if (Properties.Settings.Default.Language == 0)//中文
                    {
                        UIMessageTip.ShowOk("IPC通信成功!");
                    }
                    else if (Properties.Settings.Default.Language == 1)//英文
                    {
                        UIMessageTip.ShowOk("IPC communication success!");
                    }
                    Global.LogNet.WriteInfo(melsecMc.ToString(), "IPC通信成功!");
                    //智能线程池读取PLC数据
                    Task.Run(() =>
                    {
                        RunWork();
                    });
                }
                else
                {
                    Global.LogNet.WriteError(melsecMc.ToString(), "IPC通信失败,请检查触摸屏的网络IP地址10.10.150.(2~200)");
                    if (Properties.Settings.Default.Language == 0)//中文
                    {
                        UIMessageDialog.ShowMessageDialog("IPC通信失败,请检查触摸屏的网络IP地址10.10.150.(2~200)!", UILocalize.ErrorTitle, false, UIStyle.Red);
                    }
                    else if (Properties.Settings.Default.Language == 1)//英文
                    {
                        UIMessageDialog.ShowMessageDialog("IPC communication failure,Please check the network IP address of the touch screen 10.10.150.(2~200)", UILocalize.ErrorTitle, false, UIStyle.Red);
                    }
                    //重新连接IPC
                    Task.Run(() =>
                    {
                        Reconnect();
                    });

                    Console.WriteLine("connect failed:" + connect.Message);
                    Global.LogNet.WriteError(melsecMc.ToString(), "连接失败!原因:" + connect.Message);
                }
                //读取PLC长连接是否正常
                Task.Run(() =>
                {
                    GetPingStates();
                });
            }
            else if (appliance.ToLower() == "client")
            {
                Task.Run(() =>
                {
                    GetMqttDataRunWork();
                });
            }
        }

        private static void GetMqttDataRunWork()
        {
            while (true)
            {
                Thread.Sleep(100);//设置采集周期
                if (HslMqtt.connect.IsSuccess)
                {
                    Global.rData = Global.rMQTTData;
                }
            }
        }

        private static void Reconnect()
        {
            while (true)
            {
                //Thread.Sleep(1000);//设置采集周期
                connect = melsecMc.ConnectServer(); //连接后自动切换到长连接
                if (connect.IsSuccess)
                {
                    //智能线程池读取PLC数据
                    Task.Run(() =>
                    {
                        RunWork();
                    });
                    return;
                }
            }
        }

        private static void GetPingStates()
        {
            while (true)
            {
                var vStateFlag = melsecMc.GetPipeSocket().IsSocketError;
                if (vStateFlag == true)
                {
                    if (Properties.Settings.Default.Language == 0)//中文
                    {
                        UIMessageTip.ShowError("IPC通信失败,请检查触摸屏的网络IP地址10.10.150.(2~200)");
                    }
                    else if (Properties.Settings.Default.Language == 1)//英文
                    {
                        UIMessageTip.ShowError("IPC communication failure,Please check the network IP address of the touch screen 10.10.150.(2~200)");
                    }
                }
                Thread.Sleep(2000);//设置采集周期
            }
        }

        private static void RunWork()
        {
            while (true)
            {
                OperateResult<DataExample> read = melsecMc.Read<DataExample>();
                Thread.Sleep(100);//设置采集周期
                //写操作
                while (writeActions.TryDequeue(out Action<MelsecMcNet> action))
                {
                    action.Invoke(melsecMc);// 现在需要消费一个写入操作
                }

                if (read.IsSuccess)
                {
                    // success
                    if (con_flag == 1)
                    {
                        if (Properties.Settings.Default.Language == 0)//中文
                        {
                            UIMessageTip.ShowOk("通信重新连接成功!");
                        }
                        else if (Properties.Settings.Default.Language == 1)//英文
                        {
                            UIMessageTip.ShowOk("Communication reconnected successfully!");
                        }
                        Global.LogNet.WriteInfo(melsecMc.ToString(), "通信重新连接成功!");
                        con_flag = 0;
                    }
                }
                else
                {
                    // failed
                    con_flag = 1;
                    continue;
                }
                /**************************
                 ********解析寄存器********
                 **************************/
                //开关量
                Global.rData.gM0 = read.Content.readM0;
                Global.rData.gM100 = read.Content.readM100;
                Global.rData.gM1000 = read.Content.readM1000;
                Global.rData.gM2000 = read.Content.readM2000;
                Global.rData.gM5000 = read.Content.readM5000;
                Global.rData.gX = read.Content.readX;
                Global.rData.gY = read.Content.readY;
                Global.rData.gS0 = read.Content.readS0;
                Global.rData.gS1000 = read.Content.readS1000;
                Global.rData.gS1200 = read.Content.readS1200;
                Global.rData.gS2000 = read.Content.readS2000;
                //寄存器
                //int a = melsecMc.ByteTransform.TransInt32(read.Content.readD1000, 0);  // 为什么是200呢？为每个D占用2个字节，所以是 100*2
                //short D1100 = melsecMc.ByteTransform.TransInt16(readD2000.Content, 200);
                Global.rData.D1004 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 8);
                Global.rData.D1006 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 12);
                Global.rData.D1008 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 16);
                Global.rData.D1010 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 20);
                Global.rData.D1012 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 24);
                Global.rData.D1014 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 28);
                Global.rData.D1016 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 32);
                Global.rData.D1018 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 36);
                Global.rData.D1020 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 40);
                Global.rData.D1022 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 44);
                Global.rData.D1024 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 48);
                Global.rData.D1026 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 52);
                Global.rData.D1028 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 56);
                Global.rData.D1030 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 60);
                Global.rData.D1032 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 64);
                Global.rData.D1036 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 72);
                Global.rData.D1040 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 80);
                Global.rData.D1042 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 84);
                Global.rData.D1044 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 88);
                Global.rData.D1046 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 92);
                Global.rData.D1048 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 96);
                Global.rData.D1050 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 100);
                Global.rData.D1052 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 104);
                Global.rData.D1054 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 108);
                Global.rData.D1056 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 112);
                Global.rData.D1058 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 116);
                Global.rData.D1060 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 120);
                Global.rData.D1062 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 124);
                Global.rData.D1064 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 128);
                Global.rData.D1066 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 132);
                Global.rData.D1068 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 136);
                Global.rData.D1070 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 140);
                Global.rData.D1072 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 144);
                Global.rData.D1074 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 148);
                Global.rData.D1076 = melsecMc.ByteTransform.TransSingle(read.Content.readD1000, 152);
                Global.rData.D1100 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 200);
                Global.rData.D1102 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 204);
                Global.rData.D1104 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 208);
                Global.rData.D1106 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 212);
                Global.rData.D1108 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 216);
                Global.rData.D1110 = melsecMc.ByteTransform.TransUInt32(read.Content.readD1000, 220);
                Global.rData.D1400 = melsecMc.ByteTransform.TransInt16(read.Content.readD1000, 800);

                Global.rData.D1500 = melsecMc.ByteTransform.TransString(read.Content.readD1000, 1000, 20, Encoding.ASCII);

                Global.rData.D2000 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 0);
                Global.rData.D2002 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 4);
                Global.rData.D2004 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 8);
                Global.rData.D2006 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 12);
                Global.rData.D2008 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 16);
                Global.rData.D2010 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 20);
                Global.rData.D2012 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 24);
                Global.rData.D2014 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 28);
                Global.rData.D2016 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 32);
                Global.rData.D2018 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 36);
                Global.rData.D2020 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 40);
                Global.rData.D2022 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 44);
                Global.rData.D2024 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 48);
                Global.rData.D2026 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 52);
                Global.rData.D2028 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 56);
                Global.rData.D2030 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 60);
                Global.rData.D2032 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 64);
                Global.rData.D2034 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 68);
                Global.rData.D2036 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 72);
                Global.rData.D2038 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 76);
                Global.rData.D2040 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 80);
                Global.rData.D2042 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 84);

                Global.rData.D2050 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 100);
                Global.rData.D2054 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 108);
                Global.rData.D2056 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 112);
                Global.rData.D2058 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 116);
                Global.rData.D2060 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 120);
                Global.rData.D2062 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 124);
                Global.rData.D2066 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 132);
                Global.rData.D2068 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 136);
                Global.rData.D2070 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 140);
                Global.rData.D2072 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 144);
                Global.rData.D2074 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 148);
                Global.rData.D2076 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 152);
                Global.rData.D2078 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 156);
                Global.rData.D2080 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 160);
                Global.rData.D2082 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 164);
                Global.rData.D2084 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 168);
                Global.rData.D2086 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 172);
                Global.rData.D2088 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 176);
                Global.rData.D2090 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 180);
                Global.rData.D2092 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 184);

                Global.rData.D2108 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 216);
                Global.rData.D2110 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 220);
                Global.rData.D2112 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 224);
                Global.rData.D2114 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 228);
                Global.rData.D2116 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 232);
                Global.rData.D2118 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 236);
                Global.rData.D2120 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 240);
                Global.rData.D2122 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 244);
                Global.rData.D2124 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 248);
                Global.rData.D2126 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 252);
                Global.rData.D2128 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 256);
                Global.rData.D2130 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 260);
                Global.rData.D2132 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 264);
                Global.rData.D2134 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 268);
                Global.rData.D2136 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 272);
                Global.rData.D2138 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 276);
                Global.rData.D2140 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 280);
                Global.rData.D2142 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 284);
                Global.rData.D2144 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 288);
                Global.rData.D2146 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 292);
                Global.rData.D2148 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 296);
                Global.rData.D2150 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 300);
                Global.rData.D2152 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 304);
                Global.rData.D2154 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 308);
                Global.rData.D2156 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 312);
                Global.rData.D2158 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 316);
                Global.rData.D2160 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 320);
                Global.rData.D2162 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 324);
                Global.rData.D2164 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 328);
                Global.rData.D2166 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 332);

                Global.rData.D2170 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 340);
                Global.rData.D2172 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 344);
                Global.rData.D2174 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 348);
                Global.rData.D2176 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 352);
                Global.rData.D2178 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 356);
                Global.rData.D2180 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 360);
                Global.rData.D2182 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 364);
                Global.rData.D2184 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 368);
                Global.rData.D2186 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 372);
                Global.rData.D2188 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 376);
                Global.rData.D2190 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 380);
                Global.rData.D2192 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 384);
                Global.rData.D2194 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 388);
                Global.rData.D2196 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 392);
                Global.rData.D2198 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 396);

                Global.rData.D2200 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 400);
                Global.rData.D2202 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 404);
                Global.rData.D2204 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 408);
                Global.rData.D2206 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 412);
                Global.rData.D2208 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 416);
                Global.rData.D2210 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 420);
                Global.rData.D2212 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 424);
                Global.rData.D2214 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 428);
                Global.rData.D2216 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 432);
                Global.rData.D2218 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 436);
                Global.rData.D2220 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 440);
                Global.rData.D2222 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 444);
                Global.rData.D2224 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 448);
                Global.rData.D2226 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 452);
                Global.rData.D2228 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 456);
                Global.rData.D2230 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 460);
                Global.rData.D2232 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 464);
                Global.rData.D2234 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 468);
                Global.rData.D2236 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 472);
                Global.rData.D2238 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 476);
                Global.rData.D2240 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 480);
                Global.rData.D2242 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 484);
                Global.rData.D2244 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 488);
                Global.rData.D2246 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 492);
                Global.rData.D2248 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 496);
                Global.rData.D2250 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 500);
                Global.rData.D2252 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 504);
                Global.rData.D2254 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 508);
                Global.rData.D2256 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 512);
                Global.rData.D2258 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 516);
                Global.rData.D2260 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 520);
                Global.rData.D2262 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 524);
                Global.rData.D2264 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 528);
                Global.rData.D2266 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 532);
                Global.rData.D2268 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 536);
                Global.rData.D2270 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 540);
                Global.rData.D2272 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 544);
                Global.rData.D2274 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 548);
                Global.rData.D2276 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 552);
                Global.rData.D2278 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 556);
                Global.rData.D2280 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 560);
                Global.rData.D2282 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 564);
                Global.rData.D2284 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 568);
                Global.rData.D2286 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 572);
                Global.rData.D2288 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 576);
                Global.rData.D2290 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 580);
                Global.rData.D2292 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 584);
                Global.rData.D2294 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 588);
                Global.rData.D2296 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 592);
                Global.rData.D2298 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 596);
                Global.rData.D2300 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 600);
                Global.rData.D2302 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 604);
                Global.rData.D2304 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 608);
                Global.rData.D2306 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 612);
                Global.rData.D2308 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 616);
                Global.rData.D2310 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 620);
                Global.rData.D2312 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 624);
                Global.rData.D2314 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 628);
                Global.rData.D2316 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 632);
                Global.rData.D2318 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 636);
                Global.rData.D2320 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 640);
                Global.rData.D2322 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 644);
                Global.rData.D2324 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 648);
                Global.rData.D2326 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 652);
                Global.rData.D2328 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 656);
                Global.rData.D2330 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 660);
                Global.rData.D2332 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 664);
                Global.rData.D2334 = melsecMc.ByteTransform.TransUInt32(read.Content.readD2000, 668);

                Global.rData.D2600 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1200);
                Global.rData.D2601 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1202);
                Global.rData.D2602 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1204);
                Global.rData.D2603 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1206);
                Global.rData.D2604 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1208);
                Global.rData.D2605 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1210);
                Global.rData.D2606 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1212);
                Global.rData.D2607 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1214);
                Global.rData.D2608 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1216);
                Global.rData.D2609 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1218);
                Global.rData.D2610 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1220);
                Global.rData.D2611 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1222);
                Global.rData.D2612 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1224);
                Global.rData.D2613 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1226);
                Global.rData.D2614 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1228);
                Global.rData.D2615 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1230);
                Global.rData.D2616 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1232);
                Global.rData.D2617 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1234);
                Global.rData.D2618 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1236);
                Global.rData.D2619 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1238);
                Global.rData.D2620 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1240);
                Global.rData.D2621 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1242);
                Global.rData.D2622 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1244);
                Global.rData.D2623 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1246);
                Global.rData.D2624 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1248);
                Global.rData.D2625 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1250);
                Global.rData.D2626 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1252);
                Global.rData.D2627 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1254);
                Global.rData.D2628 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1256);
                Global.rData.D2629 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1258);
                Global.rData.D2630 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1260);
                Global.rData.D2631 = melsecMc.ByteTransform.TransUInt16(read.Content.readD2000, 1262);
                Global.rData.D2632 = melsecMc.ByteTransform.TransInt16(read.Content.readD2000, 1264);
                Global.rData.D2633 = melsecMc.ByteTransform.TransInt16(read.Content.readD2000, 1266);

                Global.rData.D2800 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1600);
                Global.rData.D2802 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1604);
                Global.rData.D2804 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1608);
                Global.rData.D2806 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1612);
                Global.rData.D2808 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1616);
                Global.rData.D2810 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1620);
                Global.rData.D2812 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1624);
                Global.rData.D2814 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1628);
                Global.rData.D2816 = melsecMc.ByteTransform.TransSingle(read.Content.readD2000, 1632);

                Global.rData.D3300 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 600);
                Global.rData.D3301 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 602);
                Global.rData.D3302 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 604);
                Global.rData.D3303 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 606);
                Global.rData.D3304 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 608);
                Global.rData.D3305 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 610);
                Global.rData.D3306 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 612);
                Global.rData.D3307 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 614);
                Global.rData.D3308 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 616);
                Global.rData.D3309 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 618);
                Global.rData.D3310 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 620);
                Global.rData.D3311 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 622);
                Global.rData.D3312 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 624);
                Global.rData.D3313 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 626);
                Global.rData.D3314 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 628);
                Global.rData.D3315 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 630);
                Global.rData.D3316 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 632);
                Global.rData.D3317 = melsecMc.ByteTransform.TransUInt16(read.Content.readD3000, 634);
                
            }
        }

        public static void WriteBool(string address, bool value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "bool",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }

        public static void WriteInt16(string address, short value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "short",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }

        }
        public static void WriteUInt16(string address, ushort value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "ushort",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }
        public static void WriteInt32(string address, int value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "int",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }

        public static void WriteUInt32(string address, uint value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "uint",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }

        public static void WriteUInt32(string address, uint value, float offset)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, Convert.ToUInt32(Convert.ToSingle(value) * offset));
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "float",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }

        public static void WriteSingle(string address, float value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "float",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }
        public static void WriteInt64(string address, long value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "long",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }
        public static void WriteUInt64(string address, ulong value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "ulong",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }
        public static void WriteDouble(string address, double value)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "double",
                    address = address,
                    value = value.ToString()
                };

                HslMqtt.personModelToMqtt(person);
            }
        }
        public static void WriteString(string address, string value, int length)
        {
            if (appliance is null)
                return;

            if (appliance.ToLower() == "server")//本地服务端
            {
                if (connect is null)
                    return;

                if (connect.IsSuccess && !melsecMc.GetPipeSocket().IsSocketError)
                {
                    Action<MelsecMcNet> action = new Action<MelsecMcNet>(melsecMc =>
                    {
                        OperateResult write = melsecMc.Write(address, value, length, Encoding.ASCII);
                        if (write.IsSuccess)
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入成功!");
                            Global.LogNet.WriteInfo(melsecMc.ToString(), "写入成功!address:" + address.ToString() + ";value:" + value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString() + " 写入失败: " + write.Message);
                            Global.LogNet.WriteError(melsecMc.ToString(), "写入失败!原因:" + write.Message);
                        }
                    });
                    writeActions.Enqueue(action);   // 生产者，放入一个写入的操作，然后交给唯一的线程去执行写入
                }
            }
            else if (appliance.ToLower() == "client")
            {
                PersonModel person = new PersonModel()
                {
                    unit = "string",
                    address = address,
                    value = value.ToString(),
                    lenth = length.ToString(),
                };

                HslMqtt.personModelToMqtt(person);
            }
        }

        public static void XMMqttWritePlc(PersonModel person)
        {
            switch (person.unit)
            {
                case "bool":
                    WriteBool(person.address.ToUpper(), Convert.ToBoolean(person.value));
                    break;
                case "short":
                    WriteInt16(person.address.ToUpper(), Convert.ToInt16(person.value));
                    break;
                case "ushort":
                    WriteUInt16(person.address.ToUpper(), Convert.ToUInt16(person.value));
                    break;
                case "int":
                    WriteInt32(person.address.ToUpper(), Convert.ToInt32(person.value));
                    break;
                case "uint":
                    WriteUInt32(person.address.ToUpper(), Convert.ToUInt32(person.value));
                    break;
                case "float":
                    WriteSingle(person.address.ToUpper(), Convert.ToSingle(person.value));
                    break;
                case "long":
                    WriteInt64(person.address.ToUpper(), Convert.ToInt64(person.value));
                    break;
                case "ulong":
                    WriteUInt64(person.address.ToUpper(), Convert.ToUInt64(person.value));
                    break;
                case "double":
                    WriteDouble(person.address.ToUpper(), Convert.ToDouble(person.value));
                    break;
                case "string":
                    WriteString(person.address.ToUpper(), person.value.ToString(), Convert.ToInt32(person.lenth));
                    break;
                default:
                    break;
            }
        }
    }
}
